% The code is based on the following paper:
%   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.
%   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies.
%
% Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng
% Date: January 30, 2024

clear
 
%% Setting            
BV_Target = {'UB'};  

%% Optimal Risk Preferences under Rank-Dependent Expected Utility
FileName = ['Summary_OPT_AllCR_AllPeriod_OP_UB'];
load(FileName, ...
     'Date_Monthly', ...
     'Index_CR', 'Index_OPT', 'OPT_TAB');
clear FileName 

OPT = nan(size(Date_Monthly, 1), length(Index_OPT));                      
for d = 1:size(Date_Monthly, 1)
    OPT(d, :) = OPT_TAB(Index_CR, Index_OPT, d);                                 
end 
clear Index_CR Index_OPT OPT_TAB
clear d

%% Four Specific Cases 
AllPeriod = [[19990317 19990416]; ...
	         [20030219 20030321]; ...
	         [20111019 20111118]; ...
	         [20090121 20090220]];
[~, Index_Location] = ismember(AllPeriod, Date_Monthly(:, [1 2]), 'rows');
Index_Location(Index_Location==0) = [];                                    
clear Date_Monthly

OPT = OPT(Index_Location, :);                                              
clear Index_Location

AllPeriod_Text = cell(size(AllPeriod, 1), 1);                              
for d = 1:size(AllPeriod, 1)
    if d < size(AllPeriod, 1)
        AllPeriod_Text(d, 1) = cellstr(char(['\qquad ' datestr(datenum(num2str(AllPeriod(d, 1)), 'yyyymmdd'), 'mmm dd, yyyy')]));
    else
        AllPeriod_Text(d, 1) = cellstr(char(['\qquad \quad ' datestr(datenum(num2str(AllPeriod(d, 1)), 'yyyymmdd'), 'mmm dd, yyyy')]));
    end
end
clear d

%% Plot Figure: Probability Weighting Function (Heterogeneous)
h_PWF = figure;
for d = 1:size(AllPeriod, 1)
    FileName = ['Summary_OP_Grid_' num2str(AllPeriod(d, 1))];
    load(FileName, ...
         'Record', 'Record_OP_LB', 'Record_OP_UB');
    clear FileName

    Index = find((Record(:, 1)==1) & ...
                 (Record(:, 2)==1));
    VIORate = 1 - Record(Index, end - 1);                                                              
    clear Index

    Index = find((Record(:, 1)==OPT(d, 1)) & ...
                 (Record(:, 2)==OPT(d, 2)));                        
    OPT_UB = Record_OP_UB(Index, :);                                     
    clear Index

    Index = logical(OPT_UB >= Record_OP_UB);
    Index = 1 - Index;                                                     
    Index = find(sum(Index, 2)==0);                                        
    OPT_All = Record(Index, :);
    OPT_All(:, (end - 2):end) = [];                                        
    clear Index OPT_UB
    clear Record Record_OP_LB Record_OP_UB

    % Plot Figure
    subplot(2, 2, d)
    
    plot(OPT(d, 1), OPT(d, 2), ...
         'LineStyle', 'None', ...
         'LineWidth', 1.5, ...
         'Marker', '*', ...
         'MarkerSize', 12, ...
         'Color', 'r')
    hold on
    grid on

    [AllAlpha, ~, Index_Alpha] = unique(OPT_All(:, 1));    
    LB = accumarray(Index_Alpha, OPT_All(:, 2), size(AllAlpha), @min, nan)';
    UB = accumarray(Index_Alpha, OPT_All(:, 2), size(AllAlpha), @max, nan)'; 
    DM = AllAlpha';       
    h_Area = fill([DM fliplr(DM)], ...
                  [LB fliplr(UB)], ...
                  [0 0.45 0.74]);
    hold on
    clear Index_Alpha AllAlpha LB UB DM
        
    set(h_Area, 'EdgeColor', [0 0.45 0.74])
    set(h_Area, 'LineWidth', 1.5)      
    clear h_Area                     

    plot(OPT(d, 1), OPT(d, 2), ...
         'LineStyle', 'None', ...
         'LineWidth', 1.5, ...
         'Marker', '*', ...
         'MarkerSize', 12, ...
         'Color', 'r')
    hold on    

    h = legend(['Investors with ($\alpha^\ast$ , $\beta^\ast$) = (' num2str(OPT(d, 1)) ' , ' num2str(OPT(d, 2)) ')'], ...
               'Other Investors');
    set(h, 'FontSize', 12, ...
           'FontName', 'Times New Roman', ...
           'FontWeight', 'Bold', ...
           'Location', 'NorthWest', ...
           'Interpreter', 'Latex', ...
           'Box', 'Off')
    clear h    
    
    h1 = xlabel('$\alpha$');  
    h2 = ylabel('$\beta$');
    h3 = title([AllPeriod_Text(d, :); '(percentage of violations under EU = ' num2str(roundn(100 * VIORate, - 2)) '\%)']); 
    set([h1 h2 h3], 'FontSize', 15, ...
                    'FontName', 'Times New Roman', ...
                    'FontWeight', 'Bold', ...
                    'Interpreter', 'Latex')
    clear h1 h2 h3    
   
    set(gca, 'XLim', [0 3], ...
             'XTick', 0:0.5:3, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')
    set(gca, 'YLim', [0 3], ...
             'YTick', 0:0.5:3, ...
             'FontSize', 15, ...
             'FontName', 'Times New Roman')     
    
    set(gca, 'Layer', 'Top')
    clear VIORate OPT_All
end
clear d

X = 29.7;                                                                  
Y = 21.0;                                                                  
XMargin = 1;                                                               
YMargin = 1;                                                               
XSize = X - 2 * XMargin;                                                   
YSize = Y - 2 * YMargin;                                                   
set(h_PWF, 'PaperUnits', 'Centimeters')
set(h_PWF, 'PaperSize', [X Y])
set(h_PWF, 'PaperPosition', [XMargin YMargin XSize YSize])
set(h_PWF, 'PaperOrientation', 'Portrait')
clear AllPeriod AllPeriod_Text AllCR_Target h_PWF X Y XMargin YMargin XSize YSize

%% Output
FileName = ['Figure_PWF_Heterogeneous_UB'];
saveas(gcf, [FileName '.eps'], 'epsc')
print(FileName, '-dpdf', '-fillpage')
winopen([FileName '.pdf'])
clear FileName